//
//  DiscoverViewController.m
//  OwnerNews
//
//  Created by lanou3g on 15/8/28.
//  Copyright (c) 2015年 Drawzyn. All rights reserved.
//

#import "DiscoverViewController.h"
#import "FindingManager.h"
#import "BodyTableViewCell.h"
#import "UIImage+ImageDownLoader.h"
#import "FindTopImage.h"
#import "FindBtnMessage.h"
#import "FindModel.h"
#import "DetialViewController.h"
#import "TopViewController.h"

@interface DiscoverViewController ()

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic,strong)FindingManager *findManager;



@end

@implementation DiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //self.automaticallyAdjustsScrollViewInsets = NO;
    _tableView =  [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height- 100) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    self.navigationItem.title = @"发现";
    
    _findManager = [FindingManager defaultManager];
    
    [_findManager GetMyDataAndLoadMyTableView:^{
        [self.tableView reloadData];
    }];
    
}
- (void)TapAction {
    [self TopViewChangeController];
}
- (void)TopViewChangeController {
    //计算当前属于哪一页
    int page = _myTopView.LscrollView.contentOffset.x / 320;
    FindTopImage *FTP = [FindingManager defaultManager].FindTopArray[page];
    TopViewController *TOPVC = [TopViewController new];
    TOPVC.KAURL = FTP.url;
    [self.navigationController pushViewController:TOPVC animated:YES];
}

#pragma UIPageControl添加的方法
-(void)PageAction:(UIPageControl *)page{
    
    NSInteger a = page.numberOfPages - 1;
    
    _myTopView.LscrollView.contentOffset = CGPointMake(a * 320, 180);
    
}

#pragma UIScrollViewDelegate的方法

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //计算当前属于哪一页
    int page = _myTopView.LscrollView.contentOffset.x / 320;
    _myTopView.PageControl.currentPage = page;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _findManager.FindBodyArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BodyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCID"];
    
    if (cell == nil) {
        
        cell = [[BodyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCID"];
        
    }
    
    [cell.shareButton addTarget:self action:@selector(ShareButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    FindModel *FM = _findManager.FindBodyArray[indexPath.row];
    
    cell.titleLabel.text = FM.name;
    
    [UIImage downLoaderImageWithURL:FM.icon andBlock:^(UIImage *image) {
        
        cell.titleView.image = image;
        
    }];
    
    NSString *imgUrlstring =  [FM.context firstObject][@"image"];
    
    [UIImage downLoaderImageWithURL:imgUrlstring andBlock:^(UIImage *image) {
        
        cell.BodyImaView.image = image;
        
    }];
    
    cell.detialLabel.text =  [FM.context firstObject][@"title"];
    
    cell.detialLabel2.text =  [FM.context firstObject][@"subtitle"];
    
    if ([FM.context firstObject][@"subbtn"] == NULL) {
        
        [cell.reallyPriceLabel setTitle:[NSString stringWithFormat:@"%@", [FM.context firstObject][@"subbtn"]] forState:UIControlStateNormal];
        
    }else{
        
        [cell.reallyPriceLabel setTitle:[NSString stringWithFormat:@"%@", [FM.context firstObject][@"btn"]] forState:UIControlStateNormal];
        
    }
    return cell;
}

- (void)ShareButtonAction {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前不可用" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    _myTopView = [[MyTopView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, _myTopView.LscrollView.frame.size.height)];
    UITapGestureRecognizer *taps =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapAction)];
    [_myTopView.LscrollView addGestureRecognizer:taps];
    _myTopView.LscrollView.delegate = self;
    [_myTopView.PageControl addTarget:self action:@selector(PageAction:) forControlEvents:UIControlEventValueChanged];
    
    
    return _myTopView;
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 180;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BodyTableViewCell *cel = [[BodyTableViewCell alloc]init];
    return [cel MyselfCellHight];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DetialViewController *deVC = [[DetialViewController alloc]init];
    deVC.indexPaths = indexPath;
    [self.navigationController presentViewController:deVC animated:YES completion:^{}];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
